/*
 * @Author: LVGRAPE
 * @LastEditors: LVGRAPE
 */
#ifndef __CH1115_H
#define __CH1115_H


#include <math.h>
#include <stdint.h>

// extern SPI_HandleTypeDef hspi1;  //spi句柄
#define PKG_USING_SSD1306_I2C_BUS_NAME "i2c1"
#define Max_time 1000            //spi发送超时时间
#define SSD1306_I2C_ADDR        0x3C
#define SSD1306_CTRL_CMD     0x00
#define SSD1306_CTRL_DATA    0x40
#define SSD1306_MASK_CONT    (0x1<<7)

#define OLED_WIDTH 128
#define OLED_HEIGHT 64
// Display Pixel colours  definition
#define OLED_WHITE  0
#define OLED_BLACK 1
#define OLED_INVERSE 2

/* CH1115 Command Set*/
// Fundamental Commands
#define ERMCH1115_ENTIRE_DISPLAY_ON 0xA4 /**< Forcibly turns the entire display on regardless of the contents of the display data RAM */
#define ERMCH1115_ENTIRE_DISPLAY_OFF 0xA5 /**< Forcibly turns the entire display on regardless of the contents of the display data RAM */
#define ERMCH1115_DISPLAY_ON 0xAF /**< Turns on Display */
#define ERMCH1115_DISPLAY_OFF 0xAE /**< Turns off Display */
#define ERMCH1115_DISPLAY_NORMAL 0xA6  /**< Display  Inversion off*/
#define ERMCH1115_DISPLAY_INVERT 0xA7  /**< Display  Inversion on*/
#define ERMCH115_CONTRAST_CONTROL 0x81 /**<  Contrast Control Mode Set */
#define ERMCH115_CONTRAST_DATA_DEFAULT 0x80  /**<  Contrast Data Reg Set 0x00 to 0xFF */

// Addressing Setting Commands
#define ERMCH1115_SET_PAGEADD 0xB0 /**< Set Page Address: (B0H - B7H) */
#define ERMCH1115_SET_COLADD_LSB 0x00 /**< Set Lower Column Address: (00H - 0FH) */
#define ERMCH1115_SET_COLADD_MSB 0x10 /**< Set Higher Column Address: (10H – 1FH) */
#define ERMCH115_SET_DISPLAY_START_LINE 0x40 /**< Determine the initial display line */

// Hardware Configuration Commands
#define ERMCH1115_SET_PUMP_REG 0x30 /**< Output voltage (V PP ) internal charger pump. */
#define ERMCH115_SET_PUMP_SET   0x01 /**<  A1:A0[] 01 = 7.4v  */
#define ERMCH1115_IREF_REG 0x82 /**< I REF Resistor Set  */
#define ERMCH1115_IREF_SET 0x00 /**< I REF Resistor adjust */
#define ERMCH1115_SEG_SET_REMAP  0xA0 /**< Set Segment Re-map: (A0H - A1H) */
#define ERMCH1115_SEG_SET_PADS 0xA2 /**<Set SEG pads hw config: (A2H – A3H) */
#define ERMCH1115_MULTIPLEX_MODE_SET 0xA8 /**<Multiplex Ration Mode Set */
#define ERMCH1115_MULTIPLEX_DATA_SET  0x3F /**<Multiplex Ration Data Set: (00H - 3FH) */

#define ERMCH1115_DC_MODE_SET 0xAD /**< DC-DC Control Mode Set */
#define ERMCH1115_DC_ONOFF_SET 0x8B /**< DC-DC ON/OFF Mode Set (8AH - 8BH) (a=off; b=on) */
#define ERMCH1115_COMMON_SCAN_DIR 0xC0 /**< Set Common Op Scan Dir:C0H - C8H Flip vertical */
#define ERMCH1115_OFFSET_MODE_SET 0xD3 /**<Display Offset Mode Set: */
#define ERMCH1115_OFFSET_DATA_SET 0x00 /**<Display Offset Data Set: (00H~3FH) */
#define ERMCCH1115_BREATHEFFECT_SET 0x23 /**< Set Breathing Display Effect: */
#define ERMCCH1115_BREATHEFFECT_DATA 0x81 /**< Display Effect ON/OFF and Time Interval: */

// Timing & Driving Scheme Setting Commands
#define ERMCH1115_OSC_FREQ_MODE_SET 0xD5 /**<Divide Ratio/Oscillator Frequency Mode Set: (D5H) */
#define ERMCH1115_OSC_FREQ_DATA_SET 0xA0  /**<Divide Ratio/Oscillator Frequency Data Set: */
#define ERMCH1115_PRECHARGE_MODE_SET 0xD9 /**<Pre-charge Period Mode Set: */
#define ERMCH1115_PRECHARGE_DATA_SET 0x22 /**<Discharge/Pre-charge Period Data Set: (00H - FFH) */
#define ERMCH1115_COM_LEVEL_MODE_SET 0xDB /**<COM Deselect Level Mode Set: (DBH) */
#define ERMCH1115_COM_LEVEL_DATA_SET 0x40 /**<VCOM Deselect Level Data Set: (00H - FFH) */


// HORIZONTAL Scrolling Commands
#define ERMCH1115_HORIZONTAL_A_SCROLL_SETUP 0x24 /**<  Additional Horizontal Scroll Setup Mode Set: (24H) */
#define ERMCH1115_HORIZONTAL_A_SCROLL_SET_SCOL 0x00  /**< Start Column Position Set: (A7 – A0) */
#define ERMCH1115_HORIZONTAL_A_SCROLL_SET_ECOL 0x7F /**< End Column Position Set: (B7 – B0) */
#define ERMCH1115_SCROLL_SETUP  0x26 /**<Horizontal Scroll Setup: 26 right 27 left (A2 – A0)*/
#define ERMCH1115_SPAGE_ADR_SET 0x00  /**< Start Page Address Set 0x00 = page 0 */
#define ERMCH1115_TIME_SET 0x00 /**<Time Interval Set: (B2 – B0) 0x00 = 6 frames */
#define ERMCH1115_EPAGE_ADR_SET  0x07 /**<End Page Address Set: (C2 – C0)  */
#define ERMCH1115_SET_SCROLL_MODE 0x28 /**< Set Scroll Mode: (28H – 2BH) 0x28 = continuous */
#define ERMCH1115_DEACTIVATE_SCROLL  0x2E /**< De-active Scroll mode */
#define ERMCH1115_ACTIVATE_SCROLL   0x2F /**<active Scroll mode */

typedef enum{           //用于计算横向字符偏移
    OLED_8X16  = 8,
    OLED_6X8   = 6,
    OLED_16X32 = 16,
}OLED_FontSize;         //字符属性

typedef enum{
    OLED_UNFILLED = 0,  //不填充
    OLED_FILLED   = 1,  //填充
    OLED_COUNTER  = 2,  //取反
}OLED_IsFilled;         //图形属性


uint32_t OLED_Pow(uint32_t X, uint32_t Y);
uint8_t OLED_IsInAngle(int16_t X, int16_t Y, int16_t StartAngle, int16_t EndAngle);
void OLED_WriteByte(uint8_t *Data, uint8_t Count);
void OLED_WriteCommand(uint8_t Command);
void OLED_WriteData(uint8_t *Data, uint8_t Count);
void OLED_Init(void);
void OLED_Update(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height);
void OLED_Clear(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height);
void OLED_ShowChar(uint8_t X, uint8_t Y, char Char, OLED_FontSize FontSize);
void OLED_ShowString(uint8_t X, uint8_t Y, char *String, OLED_FontSize FontSize);
void OLED_ShowNum(uint8_t X, uint8_t Y, uint32_t Number, uint8_t Length, OLED_FontSize FontSize);
void OLED_ShowFullNum(uint8_t X, uint8_t Y, double Number, uint8_t Int, uint8_t Fra, OLED_IsFilled IsFilled, OLED_FontSize FontSize);
void OLED_ShowChinese(uint8_t X, uint8_t Y, char *Chinese);
void OLED_ShowImage(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height, const uint8_t *Image);
void OLED_DrawPoint(uint8_t X, uint8_t Y);
void OLED_DrawLine(uint8_t X0, uint8_t Y0, uint8_t X1, uint8_t Y1);
void OLED_DrawRectangle(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height, OLED_IsFilled IsFilled);
void OLED_DrawArc(uint8_t X, uint8_t Y, uint8_t Radius, int16_t StartAngle, int16_t EndAngle, OLED_IsFilled IsFilled);
void OLED_DrawCircle(uint8_t X, uint8_t Y, uint8_t Radius, OLED_IsFilled IsFilled);
void OLED_Roundrectangle(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height, uint8_t Radius, OLED_IsFilled IsFilled);

extern uint8_t OLED_DisplayBuf[OLED_HEIGHT/8][OLED_WIDTH];
extern const uint8_t OLED_F8x16[][16];
extern const uint8_t OLED_F6x8[][6];
extern const uint8_t OLED_F16x32[][64];
extern const uint8_t Diode[];
extern const uint8_t Hello[];

#endif /*__DRIVES_OLED_H*/
